/*
 * © 2010 Autodata Solutions Company. All Rights Reserved
 * This source code is the confidential and proprietary information of Autodata Solutions Company.
 * The user shall not, in whole or in part, copy, publish, disclose or make any use of this source code
 * without the prior written consent of Autodata Solutions Company.
 */
Ext.ns('net.autodata.compare');
(function() {

    /**
    * View implementation for the InventoryInfoPanel to define how the panel is rendered
    * @class InventoryInfoView
    * @extends net.autodata.AbstractView
    * @namespace net.autodata.compare
    */
    var View = Ext.extend(net.autodata.AbstractView, {

        /**
        * @property templates
        * @type Object
        */
        templates: undefined,

        constructor: function() {
            View.superclass.ctor.apply(this, arguments);
        },

        /**
        * @method init
        */
        init: function() {
            View.superclass.init.apply(this, arguments);
            var panel = this.component;
            panel.on({
                'update': this.render,
                'destroy': this.onDestroy,
                scope: this
            });
        },

        /**
        * @method initTemplates
        */
        initTemplates: function() {
            var ts = this.templates || {};

            if (!ts.mainTpl) {
                ts.mainTpl = new Ext.XTemplate(
					'<div class="body">',
						'<div class="instock">',
							'{[ASC.message(\'net.autodata.compare.inventory.field.inventory\')]}',
							' <span class="{[this.clickableCss(values)]}">{stockCount} ',
							'{[ASC.message(\'net.autodata.compare.inventory.label.in-stock\')]}</span>',
						'</div>',
						'<div class="pricerange-title">',
							'{[ASC.message(\'net.autodata.compare.inventory.label.price-range\')]}',
						'</div>',
						'<div class="pricerange">{[this.currency(values.startPrice)]} - {[this.currency(values.endPrice)]}',
						'<tpl:if="this.hasUrl(url)">',
							'<div class="details">',
								'<span class="clickable">{[ASC.message(\'net.autodata.compare.inventory.label.see-vehicle-details\')]}</span>',
							'</div>',
						'</tpl>',
					'</div>',
					{
						
						hasUrl: function (url)
						{
							return !Ext.isEmpty(url);
						},
						
						clickableCss: function (v)
						{
							return v.url ? 'clickable' : '';
						},
						
					    currency: function(v) {
					        return v ? ASC.util.formatCurrency(v, 0) : '';
					    },

					    compiled: true,
					    disableFormats: true
					}
				);
            }

            if (!ts.emptyTpl) {
                ts.emptyTpl = new Ext.Template('<div class="nodata"></div>');
            }

            this.templates = ts;
        },

        /**
        * @method render
        */
        render: function() {
            var panel = this.component;
            var body = panel.el;
            var ir = panel.getInventoryRecord();

            if (ir) {
                this.templates.mainTpl.overwrite(body.dom, ir || {});
            } else {
                this.templates.emptyTpl.overwrite(body.dom, {});
            }
        },

        /**
        * @method onDestroy
        * @private
        */
        onDestroy: function() {
            var panel = this.component;
            panel.un({
                'update': this.render,
                'destroy': this.onDestroy,
                scope: this
            })
        }
    });

    net.autodata.compare.InventoryInfoView = View;
})();